package com.sky.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.Annotation.AutoFill;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.enumeration.OperationType;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类管理")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> List(CategoryPageQueryDTO categoryPageQueryDTO)
    {
        log.info("分类分页查询:{}",categoryPageQueryDTO);
        PageResult pageResult  = categoryService.List(categoryPageQueryDTO);
        return Result.success(pageResult);
    }


    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> selectByType(Integer type)
    {
        log.info("根据类型查询分类：{}",type);
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(Category::getType, type);
        List<Category> list = categoryService.list(categoryLambdaQueryWrapper);
        return Result.success(list);
    }
    @PutMapping
    @ApiOperation("修改分类")
    @AutoFill(OperationType.UPDATE)
    public Result update(@RequestBody  CategoryDTO categoryDTO)
    {
        log.info("修改分类：{}",categoryDTO);
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        categoryService.updateAll(category);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用分类")
    public Result updateStatusById(@PathVariable Integer status,Long id)
    {
        log.info("启用禁用分类:{}",status);
        Category category = new Category();
        category.setId(id);
        category.setStatus(status);
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());
        categoryService.updateAll(category);
        return Result.success();
    }
    @PostMapping
    @ApiOperation("新增分类")
    public Result save(@RequestBody CategoryDTO categoryDTO)
    {
        log.info("新增分类:{}",categoryDTO);
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setCreateUser(BaseContext.getCurrentId());
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());
        categoryService.save(category);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据id删除分类")
    public Result deleteById(Long id)
    {
        log.info("根据id删除分类:{}",id);
        categoryService.removeById(id);
        return Result.success();
    }
}
